package accounting_system;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class MainTable extends JTable{
    //private PersonsList persons;
    private final Model model = new Model();
    
    public MainTable(){
        super();
    }
    
    public void setPersonsList(PersonsList persons){
        
        model.setPersonsList(persons);
        setModel(model);
    }
    
    public void onChange(){
        model.fireTableDataChanged();
    }
}

class Model extends AbstractTableModel{
    private PersonsList persons;
    
    public Model(){
        //this.persons = persons;
    }
    
    public void setPersonsList(PersonsList persons){
        this.persons = persons;
    }

    @Override
    public int getRowCount() {
        return persons.getSize();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int r, int c) {
        switch (c){
            case 0: return persons.getPerson(r).getSurName();
            case 1: return persons.getPerson(r).getFirstName();
            case 2: return persons.getPerson(r).getSecondName();
            case 3: return persons.getPerson(r).getFunction();
            default: return "";    
        }
    }

    @Override
    public String getColumnName(int c) {
        String s = "";
        switch (c){
            case 0: s = "Фамилия";
                    break;
            case 1: s = "Имя";
                    break;
            case 2: s = "Отчество";
                    break;
            case 3: s = "Должность";
                    break;
        }
        //System.out.println("s = " + s);
        return s;
    }
//
//    @Override
//    public Class<?> getColumnClass(int i) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
//
//    @Override
//    public boolean isCellEditable(int i, int i1) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
//
//    @Override
//    public void setValueAt(Object o, int i, int i1) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
//
//    @Override
//    public void addTableModelListener(TableModelListener tl) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
//
//    @Override
//    public void removeTableModelListener(TableModelListener tl) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//    }
    
}